package org.jz.gae.redbook.spb.client.slate.family;

import org.jz.gae.redbook.spb.client.toolkit.Slate;
import org.jz.gae.redbook.spb.shared.FamilyEntityView;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FamilyEditPlate extends Composite
{
      private VerticalPanel rootPanel = new VerticalPanel();
	  private final Button saveButton = new Button("Сохранить");
	  private final Button cancelButton = new Button("Отмена");;
	  private FamilyPropertiesPanel familyPropertiesPanel = new FamilyPropertiesPanel();
	private Slate slate;
	private ClickHandler submitEvent; 
    private FamilyEntityView entityView;

	public FamilyEditPlate() 
	{
		SimplePanel contentTableDecorator = new SimplePanel();
	  initWidget(contentTableDecorator);
	  contentTableDecorator.setWidth("100%");
	  initilizeInterface();
      contentTableDecorator.add(rootPanel);
      
      saveButton.addClickHandler(new ClickHandler(){
    	  @Override
    	  public void onClick(ClickEvent event)
          {
              submitOperation();
    	  }
      });
      cancelButton.addClickHandler(new ClickHandler(){
    	  @Override
    	  public void onClick(ClickEvent event)
          {
              cancelOperation();
    	  }
      });
	}

	private void initilizeInterface()
	{
		rootPanel.setWidth("100%");
		familyPropertiesPanel.setWidth("100%");
		rootPanel.add(familyPropertiesPanel);
	    HorizontalPanel menuPanel = new HorizontalPanel();
	    menuPanel.add(saveButton);
	    menuPanel.add(cancelButton);
	    rootPanel.add(menuPanel);
	}

	public void editEntityView(
			Slate _Slate,
		    String _FamilyId,	
			ClickHandler _SubmitEvent)
	{
		slate = _Slate;
		slate.getContainerAgent().getRpcService().getFamily(_FamilyId, new AsyncCallback<FamilyEntityView>(){
			@Override
			public void onFailure(Throwable caught)
			{
				Window.alert("Ошибка загрузки семейства : " + caught.getMessage());
			}

			@Override
			public void onSuccess(FamilyEntityView result)
			{
				entityView = result;
				familyPropertiesPanel.displayInformation(entityView);
			}
		});
		submitEvent = _SubmitEvent;
		slate.exposePlate(this);
	}
	
	private void cancelOperation()
	{
		slate.closeTopPlate();
	}
	
	private void submitOperation()
	{
		try
		{
			FamilyEntityView familyEntityView = familyPropertiesPanel.pickUpInformation();
			familyEntityView.setFamilyKey(entityView.getFamilyKey());
		    slate.getContainerAgent().getRpcService().updateFamily(
		    		familyEntityView, 
		    		new AsyncCallback<FamilyEntityView>() {
				        public void onSuccess(FamilyEntityView result) 
				        {
				            slate.closeTopPlate();
				            submitEvent.onClick(new ClickEvent(){});
				        }
				        public void onFailure(Throwable caught) 
				        {
				             Window.alert("Error updating family");
				        }
				    });
		}
		catch(Exception ex)
		{
            Window.alert("Invalid data : " + ex.getMessage());
		}
		
	}
}